function loadconfig(filePath, hFigure)
%LOADCONFIG loads the configuration from the config.mat file to the GUI
%   Load the configuration from the config file and diplay it ont the
%   config GUI.
%
%   INPUT ARGUMENTS
%   filePath: file path of the config file;
%   hFigure: the handle of the config GUI;
%
%   loadconfig(filePath, HFigure);

%   Copyright 2012-2015 Kang M.X. Beijing Normal University.
%   $Revision: 1.0.0 $ $Date: 2014/4/23 11:05:00 $

%% Load the config file from the file specified by filePath
load(filePath, '-mat');

%% Show the configuration on the config GUI

% Get the handle of the config GUI
handles = guidata(hFigure);  % get the handles of the config GUI.

% Load the amplifier settings
switch Config.Amplifier.SamplingRate
    case 200
        set(handles.popSamplingRate, 'Value', 1);
    case 250
        set(handles.popSamplingRate, 'Value', 2);
    case 300
        set(handles.popSamplingRate, 'Value', 3);
    case 500
        set(handles.popSamplingRate, 'Value', 4);
    case 5000
        set(handles.popSamplingRate, 'Value', 5);
    otherwise
        error('UTILITIES:LOADCONFIG:INVALIDSCONFIGURATION', ...
            'No such sampling rate available!');
end

% Switch between 32 channels and 64 channels
if Config.Amplifier.BrainAmpNo == 1
    set(handles.popBrainAmpNo, 'Value', 1);
    Channels = Config.Channels(1:32, :);
elseif Config.Amplifier.BrainAmpNo == 2
    set(handles.popBrainAmpNo, 'Value', 2);    
    Channels = Config.Channels;
else
    error('UTILITIES:LOADCONFIG:INVALIDSCONFIGURATION', ...
        'No such sampling rate available!');
end

set(handles.txtRealtimeWindowLength, ...
    'String', num2str(Config.Amplifier.RealtimeWindowLength));

% Load the SyncBox settings
if Config.SyncBox.Enable
    set(handles.chkEnableSyncBox, ...
        'Value', get(handles.chkEnableSyncBox, 'Max'));
    set(handles.txtScannerClockFrequency, 'Enable', 'on');
else
    set(handles.chkEnableSyncBox, ...
        'Value', get(handles.chkEnableSyncBox, 'Min'));
    set(handles.txtScannerClockFrequency, 'Enable', 'off');
end
set(handles.txtScannerClockFrequency, ...
    'String', num2str(Config.SyncBox.ScannerClockFrequency));

% ATTENTION: it is DISABLE_SyncMarker!
if Config.SyncBox.DisableSyncMarker
    set(handles.chkDisableSyncMarker, ...
        'Value', get(handles.chkDisableSyncMarker, 'Max'));
    set(handles.txtSyncMarkerUpdateInterval, 'Enable', 'off');
else
    set(handles.chkDisableSyncMarker, ...
        'Value', get(handles.chkDisableSyncMarker, 'Min'));

    set(handles.txtSyncMarkerUpdateInterval, 'Enable', 'on');

end
set(handles.txtSyncMarkerUpdateInterval, ...
    'String', num2str(Config.SyncBox.SyncMarkerUpdateInterval));

% Load the Recording Montages
set(handles.tblChannelSelection, 'UserData', Config.Channels);
set(handles.tblChannelSelection, 'Data', Channels);


% Load the Storage settings
set(handles.txtStoragePath, ...
    'String', Config.Storage.StoragePath);

% Load the Filter settings
% If you add some items to the filter settings, just add entries to the
% following table.
HighCutoffTable = {0, 10, 20, 30, 50, 70, 100, 200, 500, 1000, 2000};
HighCutoffIndex = 1;
for HighCutoff = HighCutoffTable
    if cell2mat(HighCutoff) == Config.Filter.HighCutoff
        break;
    end
    HighCutoffIndex = HighCutoffIndex + 1;
end
set(handles.popHighCutoff, 'Value', HighCutoffIndex);

LowCutoffTable = {0, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5};
LowCutoffIndex = 1;
for LowCutoff = LowCutoffTable
    if cell2mat(LowCutoff) == Config.Filter.LowCutoff
        break;
    end
    LowCutoffIndex = LowCutoffIndex + 1;
end

set(handles.popLowCutoff, 'Value', LowCutoffIndex);

if Config.Filter.NotchFilter.Enable
    set(handles.chkNotchFilter, ...
        'Value', get(handles.chkNotchFilter, 'Max'));
    
    set(handles.popNotchFilter, 'Enable', 'on');
    if Config.Filter.NotchFilter.Frequency == 50
        set(handles.popNotchFilter, 'Value', 1);
    elseif Config.Filter.NotchFilter.Frequency == 60
        set(handles.popNotchFilter, 'Value', 2);
    else
        error('UTILITIES:LOADCONFIG:INVALIDSCONFIGURATION', ...
            'No such Notch Filter Frequency available!'); 
    end 
else
    set(handles.chkNotchFilter, ...
        'Value', get(handles.chkNotchFilter, 'Min'));
    set(handles.popNotchFilter, 'Enable', 'off');
end

end
